package cn.wizzer.app.web.modules.controllers.open;

import cn.wizzer.app.sys.modules.models.SysMsg;
import cn.wizzer.app.sys.modules.services.SysMsgService;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.UserRecharge;
import cn.wizzer.app.user.modules.models.UserWithdraw;
import cn.wizzer.app.user.modules.models.dto.ElephantRechargeDTO;
import cn.wizzer.app.user.modules.models.dto.ElephantWithdrawDTO;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.user.modules.services.account.UserRechargeService;
import cn.wizzer.app.user.modules.services.account.UserWithdrawService;
import cn.wizzer.framework.util.JSONUtil;
import cn.wizzer.notify.WkNotifyUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang.time.DateFormatUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

@IocBean
@At("/open/elephant")
public class ApiElepahantController {

    private static final Log log = Logs.get();

    @Inject
    @Reference
    private UserRechargeService userRechargeService;
    @Inject
    @Reference
    private UserWithdrawService withdrawService;

    @Inject
    @Reference
    private UserService userService;

    @Inject
    @Reference
    private SysMsgService sysMsgService;

    @Inject
    @Reference
    private WkNotifyUtil wkNotifyUtil;

    @At("/recharge/notify")
    @Ok("raw:txt")
    public String rechargeNotify(ElephantRechargeDTO elephant) throws Exception {
        log.info("elephantRechargeNotify:" + JSONUtil.toJSONString(elephant));
        UserRecharge recharge = userRechargeService.elephantNotify(elephant);
        // 充值成功给管理员发通知
        if (null != recharge) {
            User user = userService.fetch(recharge.getUserId());
            //构建消息通知
            SysMsg msg = sysMsgService.createMsg("system", "充值消息，来自第三方通知",
                    String.format("<p>消息内容:单号：[%s]。用户：[%s]。邮箱：[%s]。时间：<span style='color:red'>%s</span>。金额：[%s]。" +
                                    "附加留言：[%s]。真实姓名[%s]。状态：[%s]。" +
                                    "<a href=%s style='color:blue'>点击跳转</a></p>",
                            recharge.getNo(), user.getUserName(), user.getEmail(),
                            DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"),
                            recharge.getAmount(), recharge.getSign(), recharge.getPayer(), recharge.getStatus().getName(),
                            "/platform/user/account/recharge?no=" + recharge.getNo()),
                    null);
            //插入通知
            sysMsgService.saveMsg(msg, null);
            //后台通知管理员
            wkNotifyUtil.notify(msg, null);
        }
        return "success";
    }

    @At("/withdraw/notify")
    @Ok("raw:txt")
    public String withdrawNotify(ElephantWithdrawDTO elephant) throws Exception {
        log.info("elephantWithdrawNotify:" + JSONUtil.toJSONString(elephant));
        UserWithdraw withdraw = withdrawService.elephantNotify(elephant);
        // 提现成功给管理员发通知
        if (null != withdraw) {
            User user = userService.fetch(withdraw.getUserId());
            // 给后台管理员发消息
            SysMsg msg = sysMsgService.createMsg("system", "提现消息，来自第三方通知",
                    String.format("<p>订单号：[%s]。用户名：[%s]。用户邮箱：[%s]。时间：<span style='color:red'>%s</span>。提现金额：[%s]。" +
                                    "状态：[%s]。<a href=%s style='color:blue'>点击跳转</a></p>",
                            withdraw.getNo(), user.getUserName(), user.getEmail(),
                            DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"),
                            withdraw.getAmount(), withdraw.getStatus().getName(), "/platform/user/account/withdraw?no=" + withdraw.getNo()),
                    null);
            //插入通知
            sysMsgService.saveMsg(msg, null);
            //后台通知管理员
            wkNotifyUtil.notify(msg, null);
        }
        return "success";
    }

}
